var Q = require('q'),
   
    barcodeMasterController = require('../../../Barcode/server/controllers/BarcodeMaster.server.controller'),
    assemblyRelationController = require('../../../Barcode/server/controllers/AssemblyRelation.server.controller');
//芯片失效，取此芯片主档装配关系中最新的条码主档信息
exports.getNewBarcodeData = function (req, res) {
    var parameter = req.body.Parameter,
        initBarcodeMaster = parameter.initBarcodeMaster;
    //初始条码的芯片失效，去装配关系表找最近的装配条码    
    assemblyRelationController.findAllByBarcodeMasterAndAssemblyType(initBarcodeMaster, function (err, result) {
        // console.log("getBarcodeData-------------"+result);
        if (err) {
            res.json({
                Data: null,
                Error: err
            });
        } else {
            if (result == null) {
                var newErr = new Error();
                newErr.leval = 9;
                newErr.title = '查询装配条码失败';
                newErr.message = '未找到对应的装配条码！';
                res.json({
                    Data: null,
                    Error: newErr
                });
            } else {
                
                barcodeMasterController.findBarcodeMasterByBarcodeMaster(result[0].AssemblyBarcode, function(err,result){
                   if (err) {
                        res.json({
                            Data: null,
                            Error: err
                        });
                    } else {
                        if (result == null) {
                            var newErr = new Error();
                            newErr.leval = 9;
                            newErr.title = '查询条码失败';
                            newErr.message = '未找到对应的条码！';
                            res.json({
                                Data: null,
                                Error: newErr
                            });
                        } else {
                            res.json({
                                Data: result,
                                Error: null
                            });
                        }
                    }
                });
            }
        }
    });
};

//提交确认
//newBarcode新芯片
//BarcodeMasterData(当前显示的)最新的条码信息
//initBarcodeMaster初始Barcode(newBarcode和initBarcode进行绑定)方法
exports.submitBarcodeData = function (req, res) {
    
    var parameter = req.body.Parameter,
        BarcodeMasterData = parameter.BarcodeMasterData,
        newBarcode = parameter.newBarcode,
        initBarcodeMaster = parameter.initBarcodeMaster;
    //当前显示的Barcode,为了给它状态置为失效
    var nowBarcode = BarcodeMasterData.Barcode;
    //存新增条码的id
    var newBarcodeMaster = 0;

    //要存入的新芯片条码主档信息    
    var newBarcodeData = BarcodeMasterData;
    delete newBarcodeData._id;
    newBarcodeData.Barcode = newBarcode;
    newBarcodeData.IsAssembly = true;
    
    barcodeMasterController.createBarcodeMaster(newBarcodeData, function (err, result) { 
      
        if (err) {
            res.json({
                Data: null,
                Error: err
            });  
        } 
        else {
            if(result!=null){
                //成功返回添加好的数据
                newBarcodeMaster = result._id;
                //更新当前芯片主档信息状态为失效
                barcodeMasterController.updateByValue(nowBarcode, { Status:5 }, function (err, result) {
                     
                    if (err) {
                        res.json({
                            Data: null,
                            Error: err
                        });
                    }
                    else{
                        if (result.ok === 1) {
                            var dateNow = Date.now();
                            var assemblyRecord = {
                                BarcodeMaster:initBarcodeMaster,
                                AssemblyBarcode:newBarcodeMaster,
                                AssemblyType:2,
                                CollectDateTime:dateNow
                            }
                            
                            //新芯片和初始芯片创建装配关系
                            assemblyRelationController.createAssemblyRelation(assemblyRecord,function(err, result){
                                
                                if (err) {
                                    res.json({
                                        Data: null,
                                        Error: err
                                    });  
                                }
                                else{
                                    
                                    if(result!=null){
                                      res.json({
                                         Data: true,
                                         Error: null
                                      });  
                                    }
                                    else{
                                        res.json({
                                           Data: false,
                                           Error: null
                                        }); 
                                    }
                                    
                                }
                            });

                        }
                    }
                });
            }
            
        }
    });
};


